package com.kc.sjwh.action;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.kc.sjwh.entity.bo.RyBo;
import com.kc.sjwh.entity.po.Ry;
import com.kc.sjwh.entity.po.User;
import com.kc.sjwh.entity.vo.table.Table;
import com.kc.sjwh.entity.vo.table.Td;
import com.kc.sjwh.entity.vo.table.Tr;
import com.kc.sjwh.other.AjaxRes;
import com.kc.sjwh.other.Page;
import com.kc.sjwh.service.OutService;

/**
 * 供外部调用的action
 * @author zhangguihua(hua7381@163.com)
 * date: 2017年9月28日
 */
@Controller
@RequestMapping("out")
public class OutAction extends BaseAction {
	static Logger logger = Logger.getLogger(OutAction.class);
	
	@Autowired
	OutService service;
	
	//打开-用户选择器
	@RequestMapping("userChooser")
	public String userChooser(HttpServletRequest req) {
		return "out/userChooser";
	}
	
	//用户选择器-分页查询
	@ResponseBody
	@RequestMapping("userChooser_getPage")
	public String userChooser_getPage(HttpServletRequest req, int pageSize, int pageNum) {
		Page<User> page = service.userChooser_getPage(pageSize, pageNum, super.getParamMap(req));
		
		//数据转表格table
		Table table = new Table();
		table.add(new Tr("<label><input type='checkbox' onclick='checkAll(this)'/> 全选</label>", 
				"创建时间", "名称", "联系电话", "所属单位", "操作"));//表头
		for(User one : page.getList()) {//数据行
			table.add(new Tr(
				new Td("<input type='checkbox' value='"+one.getId()+"'/>"),
				new Td(new SimpleDateFormat("yyyy/MM/dd HH:mm").format(one.getCjsj())),
				new Td(one.getMc()),
				new Td(one.getDh()),
				new Td(one.getSsdwmc()),
				new Td("<a onclick=\"chooseOne('"+one.getId()+"')\">选择</a>")
			));
		}
		
		//返回结果
		Map data = new HashMap();
		data.put("total", page.getTotal());
		data.put("html", table.toString());
		return JSON.toJSONString(new AjaxRes(data));
	}
	
	//打开-人员选择器
	@RequestMapping("ryChooser")
	public String ryChooser(HttpServletRequest req, Boolean multi) {
		req.setAttribute("multi", multi);
		return "out/ryChooser";
	}
	
	//人员选择器-分页查询
	@ResponseBody
	@RequestMapping("ryChooser_getPage")
	public String ryChooser_getPage(HttpServletRequest req, int pageSize, int pageNum, Boolean multi) {
		Page<Ry> page = service.ryChooser_getPage(pageSize, pageNum, super.getParamMap(req));
		StringBuffer sb = new StringBuffer();
		//数据转表格table
		for(Ry one : page.getList()) {//数据行
			RyBo bo = (RyBo)one;
			sb.append("<tr>");
			if(multi!=null && multi) {
				sb.append("<td><input type='checkbox' value='"+one.getRybh()+"'/></td>");
			}
			sb.append("<td>"+chck(bo.getSsdwmc())+"</td>");
			sb.append("<td>"+chck(bo.getRyjh())+"</td>");
			sb.append("<td>"+chck(bo.getRyxm())+"</td>");
			sb.append("<td>"+chck(bo.getXbms())+"</td>");
			sb.append("<td>"+chck(bo.getZwms())+"</td>");
			sb.append("<td><a onclick=\"chooseOne('"+one.getRybh()+"')\">选择</a></td>");
			sb.append("</tr>");
		}
		
		//返回结果
		Map data = new HashMap();
		data.put("total", page.getTotal());
		data.put("html", sb.toString());
		return JSON.toJSONString(new AjaxRes(data));
	}

	private Object chck(String o) {
		return o==null?"-":o;
	}
	
	//根据ids获取人员s
	@ResponseBody
	@RequestMapping("getRysByIds")
	public String getRysByIds(String ids) {
		List<Ry> list = service.getRysByIds(ids);
		return JSON.toJSONString(new AjaxRes(list));
	}
	
}
